package com.lry.basic.juc;

import io.lettuce.core.resource.Delay;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/9/10
 */
//订单送达发短信，手机号，转号
public class OrderDelivery implements Delayed {

    private final static long DELAY = 1*10*1000L;//默认延迟10分钟
    private String phone;
    private String transferNumber;
    private long start;

    public OrderDelivery(String phone,String transferNumber) {
        this.start = System.currentTimeMillis();
        this.phone = phone;
        this.transferNumber = transferNumber;
    }

    /**
     * 用于延迟队列内部比较排序   当前时间的延迟时间 - 比较对象的延迟时间
     * @param o
     * @return
     */
    @Override
    public int compareTo(Delayed o) {
        OrderDelivery o1 = (OrderDelivery) o;
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }


    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert((start+DELAY) - System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }

    @Override
    public String toString() {
        return "OrderDelivery{" +
                "phone='" + phone + '\'' +
                ", transferNumber='" + transferNumber + '\'' +
                ", start=" + start +
                '}';
    }
}
